/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seagate.app.servlet;

import com.seagate.app.bean.MailGroupBean;
import com.seagate.app.manager.MailListManager;
import com.seagate.app.report.IncommingBinExcel;
import com.seagate.util.mail.MailUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 *
 * @author 452530
 */
public class SendMailServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String bin = request.getParameter("binid");
            String status = request.getParameter("status");
            String sendmail = request.getParameter("sendmail");
            if ("sendmail".equalsIgnoreCase(sendmail)) {
                String subject = request.getParameter("subject");
                if("".equalsIgnoreCase(subject)||subject==null){
                    subject="FDB BIN REPORT";
                }
                String gmail = request.getParameter("gmail");
                if("".equalsIgnoreCase(gmail)||gmail==null){
                    gmail="itfis-Teparuk@seagate.com";
                }


                String group = request.getParameter("group");
                String mailList = request.getParameter("selectlist");
                MailListManager lm = new MailListManager();
                //List maillist = lm.getMailListByGroupID(Integer.parseInt(group));
                if (!("".equalsIgnoreCase(mailList))) {

                    mailList=mailList.substring(1);
//                    for (int i = 0; i < maillist.size(); i++) {
//                        if(i==0){
//                        mailList = ""+maillist.get(i);
//                        }else{
//                        mailList = mailList + "," + maillist.get(i);
//                        }
//                    }
                    HSSFWorkbook wb = new HSSFWorkbook();
                    if ("incomplete".equalsIgnoreCase(status)) {

                        if ("all".equalsIgnoreCase(bin) || bin == null) {
                            IncommingBinExcel excl = new IncommingBinExcel();
                            wb = excl.IncommingBinExcelReportNotCmp();

                        } else {
                            IncommingBinExcel excl = new IncommingBinExcel();
                            System.out.println(bin);
                            wb = excl.BinExcelReportNotCmp(bin);


                        }
                    } else {
                        if ("all".equalsIgnoreCase(bin) || bin == null) {
                            IncommingBinExcel excl = new IncommingBinExcel();
                            wb = excl.IncommingBinExcelReport();

                        } else {
                            IncommingBinExcel excl = new IncommingBinExcel();
                            System.out.println(bin);
                            wb = excl.BinExcelReport(bin);

                        }
                    }

                    MailUtil mailutils = new MailUtil();
                    String mailbody="This's E-mail send to you for update status of STTP's incoming store experiment. (Some wait for slider partner quite long time.This triggering E-mail try to get some attention from you for those BIN. The attachment is containing experiment detail about how experiment get ready, how long they have already had waited for partner slider."
                            +"<br/><b>Remark:</b><b style=\"color:yellow;\" >Yellow </b>will define that the slider partner's gap is between 2-3 days"
                            +"<br/><b style=\"color:red;\" >Red</b> will define that slider gap is more than 3 days";
                    mailutils.sendMail(subject,mailbody , gmail, mailList, wb);
                } else {
                    throw new NullPointerException("NULL MAIL LIST");
                }
            }
            out.println("{\"success\":" + true + "}");
        } catch (Exception ex) {
            out.println("{\"success\":" + false + "}");
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
